package com.gitee.wsl.flow

import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ProducerScope
import kotlinx.coroutines.channels.consumeEach
import kotlinx.coroutines.flow.FlowCollector

class ProxyCollector<T>(private val channel: Channel<T>) : FlowCollector<T> {
    override suspend fun emit(value: T) {
        channel.send(value)
    }

    suspend fun collectTo(scope: ProducerScope<T>) {
        channel.consumeEach { elem ->
            scope.send(elem)
        }
    }

    fun close() {
        channel.close()
    }
}